//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Reflection;
using OpenLS.Core.Serialization;
using OpenLS.Drawing;
using OpenLS.Drawing.Serialization;
using OpenLS.Spreadsheet.Serialization;

namespace OpenLS.Spreadsheet.Drawing
{
    public class Drawing : IOfficeXmlSerializable, INotifyPropertyChanged //\\pb: name = namespace
    {
        public Drawing(Worksheet worksheet)
        {
            this.worksheet = worksheet;
        }
        


        private readonly Worksheet worksheet;

        public Worksheet Worksheet
        {
            get { return worksheet; }
        }

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read("wsDr"))
            {
                bool broken = false;
                while (!broken)
                {
                    if (c.GetData<IShapeReader>() == null)
                        c.AddData<IShapeReader>(new ShapeHelper());
                    switch (c.ReaderLocalName)
                    {
                        case "twoCellAnchor":
                            Anchors.Add(c.ReadElement<TwoCellAnchor>(delegate { return new TwoCellAnchor(Worksheet); }));

                            break;
                        case "oneCellAnchor":
                            Anchors.Add(c.ReadElement<OneCellAnchor>(delegate { return new OneCellAnchor(Worksheet); }));
                            break;
                        case "absoluteAnchor":
                            Anchors.Add(c.ReadElement<AbsoluteAnchor>(delegate { return new AbsoluteAnchor(Worksheet); }));
                            break;
                        case "AlternateContent":
                            this.alternateContents.Add(c.ReadElement<ExtensionElement>());
                            break;
                        case null: broken = true; break;
                        default:
                            throw new NotSupportedException();
                    }
                }
            }
        }
        List<ExtensionElement> alternateContents = new List<ExtensionElement>();

        private readonly ObservableCollection<IOfficeXmlSerializable> _anchors = new ObservableCollection<IOfficeXmlSerializable>();

        public ObservableCollection<IOfficeXmlSerializable> Anchors
        {
            get { return _anchors; }
            //set { anchors = value; }
        }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        void NotifyPropertyChanged(string propertyName)
        {
            Contract.Requires(GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) != null);
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

        #region IOfficeXmlSerializable Members

        public void WriteXml(WriteContext context)
        {
            using (WriteContext wsDrE = context.Write(SpreadsheetConstants.SpreadsheetDrawingNamespace+ "wsDr"))
            {
                wsDrE.DeclareNamespace("a", DrawingConstants.DrawingNamespaceUri);
                /*wsDrE.Writer.WriteStartAttribute(null, "a", "http://www.w3.org/2000/xmlns/");
                wsDrE.Writer.WriteString(DrawingConstants.DrawingNamespaceUri);
                wsDrE.Writer.WriteEndAttribute(); */
                foreach (Anchor a in Anchors)
                    wsDrE.WriteElement(a);
            }
        }

        #endregion

        internal void ReadXml(ReadContext drawingc)
        {
            IOfficeXmlSerializable s = this;
            s.ReadXml(drawingc);
        }
    }
}
